package com.bootdo.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.common.controller.BaseController;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.system.domain.Parents;
import com.bootdo.system.service.ParentsService;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RequestMapping("/sys/parents")
@Controller
@Api(value = "/sys/parents", description = "前台用户操作接口")
public class ParentsController extends BaseController {
	String prefix = "sys/parents";
	@Autowired
	ParentsService service;


	@ApiOperation(value="客户查询", notes="根据条件查询客户", response=String.class)
	@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "客户id", required = false, paramType="query",dataType = "Long"),
        @ApiImplicitParam(name = "name", value = "客户姓名", required = false, paramType="query",dataType = "String"),
        @ApiImplicitParam(name = "nickName", value = "客户昵称", required = false,paramType="query", dataType = "Long"),
        @ApiImplicitParam(name = "pageNum", value = "当前第几页", required = true,paramType="query", dataType = "int"),
        @ApiImplicitParam(name = "pageSize", value = "每页显示多少条", required = true,paramType="query", dataType = "int")
	})
	@GetMapping("/list")
	@ResponseBody
	R list(@RequestParam Map<String, Object> param)  throws Exception{
		try {
			Query query = new Query(param);
			List<Parents> babys = service.list(query);
			PageInfo<Parents> pageInfo = new PageInfo<Parents>(babys);
			int total = (int)pageInfo.getTotal();
			PageUtils pageUtil = new PageUtils(babys, total);
			return new R("list",pageUtil);
		}catch(Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@ApiOperation(value="前台用户详情", notes="根据条件查询前台用户", response=String.class)
	@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "前台用户id", required = true,paramType="query",dataType = "Long"),
	})
	@GetMapping("/edit")
	@ResponseBody
	R edit(Long id)  throws Exception{
		return new R("obj",service.get(id));
	}

	@ApiOperation(value="前台用户保存", notes="保存前台用户", response=String.class)
	@PostMapping("/save")
	@ResponseBody
	R save(@RequestBody Parents baby)  throws Exception{
		if (service.save(baby) > 0) {
			return R.saveOk(baby.getId().toString());
		} else {
			return R.error(1, "保存失败");
		}
	}

	@ApiOperation(value="前台用户更新", notes="更新前台用户", response=String.class)
	@PostMapping("/update")
	@ResponseBody
	R update(@RequestBody Parents baby)  throws Exception{
		if (service.update(baby) > 0) {
			return R.ok();
		} else {
			return R.error(1, "更新失败");
		}
	}

	@ApiOperation(value="前台用户删除", notes="根据前台用户id删除", response=String.class)
	@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "前台用户id", required = true,paramType="query", dataType = "Long"),
	})
	@GetMapping("/remove")
	@ResponseBody
	R remove(Long id)  throws Exception{
		if (service.remove(id) > 0) {
			return R.ok();
		} else {
			return R.error(1, "删除失败");
		}
	}
}
